/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cassandrawindow;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

import java.util.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
 *
 * @author MT140QuyenNB
 */

public class PhuongThreadV1 implements Runnable{
    
    //String[] keys ;
    CassandraAction cassAct ;
    String dayStart = "";
    //String[] values;
    String dayLast = "";
    String cclick = "";
    //String[] values;
    String cview = "";    
    String bannerid = "";    
    Thread runner;
    StringBuilder sb;

    public PhuongThreadV1(CassandraAction cassActValue,String banneridValue, String dayStartValue,String dayLastValue, String cclickValue,String cviewValue,StringBuilder sbValue)
    {
        cassAct = cassActValue;
        dayStart = dayStartValue;
        dayLast = dayLastValue;
        cclick = cclickValue;
        cview = cviewValue;        
        bannerid = banneridValue;
        sb = sbValue;
        runner = new Thread(this);
        runner.start();
    }

    public void run()
    {        
            try {                
                handleData(cassAct,bannerid, dayStart, dayLast, cclick, cview,sb);
                //Thread.sleep(5*1000);
            } catch (Exception ex){}
       
    }

    public static void handleData(CassandraAction cassAct,String key,String changeDay,String endDay, String ColumFamlyCampaignClickCountCf,String ColumFamlyCampaignViewCountCf,StringBuilder sb) throws ParseException
    {
        //StringBuilder sbff = new StringBuilder();
        if(key.length() > 1)
        {
            String str = getClickCampaignHour(cassAct, changeDay, changeDay, key, ColumFamlyCampaignClickCountCf, ColumFamlyCampaignViewCountCf);
            sb.append(str);
        }else {
            Debug.println(key);
            Debug.println("fail");           
        }
    }
    
    public static String getClickCampaignHour(CassandraAction cassAct,String startDay,String finishDay,String key,String columnFamilyClick,String columnFamilyView) throws ParseException{

        StringBuilder sb = new StringBuilder();
        String dayStart = startDay.split("/")[0];
        String dayLast = finishDay.split("/")[0];
        String prefix = "-" +startDay.split("/")[1]+"-"+startDay.split("/")[2];

        String prefixDB = startDay.split("/")[2] + "-" + startDay.split("/")[1] + "-";

        for(int i = Integer.valueOf(dayStart) ; i <= Integer.valueOf(dayLast) ;i++)
        {
            for(int hour =0 ;hour <=23 ;hour ++)
            {
                if(hour < 10)
                {
                    String date = "0"+ hour +":00:00 " + String.valueOf(i) + prefix ;
                    long start_time = TimeUtil.longToDateWithHour(date);
                    //them 1h
                    long finish_time = start_time + 60*60*1000;
                    long countClick = cassAct.getCountClickByIntervalTime(key, columnFamilyClick, start_time, finish_time);
                    long countView = cassAct.getCountClickByIntervalTime(key, columnFamilyView, start_time, finish_time);
                    String str = "(" + key + "," + hour + "," + countClick + "," + countView + ",'" + (prefixDB +i) + "')";
                    sb.append(str).append(",");
                    //mysqldatamanager.insertDataCampainHour(key,hour, String.valueOf(countClick), String.valueOf(countView), (prefixDB +i));
                } else {
                    String date = hour +":00:00 " + String.valueOf(i) + prefix ;
                    long start_time = TimeUtil.longToDateWithHour(date);
                    //them 1h
                    long finish_time = start_time + 60*60*1000;
                    long countClick = cassAct.getCountClickByIntervalTime(key, columnFamilyClick, start_time, finish_time);
                    long countView = cassAct.getCountClickByIntervalTime(key, columnFamilyView, start_time, finish_time);
                    String str = "(" + key + "," + hour + "," + countClick + "," + countView + ",'" + (prefixDB +i) + "')";
                    sb.append(str).append(",");
                    //mysqldatamanager.insertDataCampainHour(key,hour, String.valueOf(countClick), String.valueOf(countView), (prefixDB +i));
                }
            }
        }
        return sb.toString();
    }


}

